import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.HashSet;
import java.util.Set;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Settings;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSCharacter;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(name="Guild Ranger", authors={"Vastico"}, description="Trains range at the ranging guild on the targets.", version=1.7D, keywords={"Combat", "Ranged"})
public class GuildRanger extends Script
  implements PaintListener
{
  private Set<GuildRanger.Action> actions;
  private long startTime = 0L;
  private int startXp = 0;
  private int startLvl = 0;
  private RSNPC judge;
  private GuildRanger.Action action;
  private boolean failsafe = false;

  public boolean onStart()
  {
    this.actions = new HashSet();

    this.actions.add(new GuildRanger.1(this));

    this.actions.add(new GuildRanger.2(this));

    this.actions.add(new GuildRanger.3(this));

    this.actions.add(new GuildRanger.4(this, GuildRanger.GameConstants.SAFE_AREA, "safe area"));

    this.actions.add(new GuildRanger.5(this, GuildRanger.GameConstants.SHOOTING_AREA, "shooting area"));

    this.actions.add(new GuildRanger.6(this, 2513, "Fire-at"));

    this.actions.add(new GuildRanger.7(this, 693, "Compete"));

    return true;
  }

  public int loop()
  {
    this.mouse.setSpeed(random(6, 8));
    if (this.camera.getPitch() > 1) {
      this.camera.setPitch(false);
    }
    if (this.action != null) {
      if (this.action.isValid()) {
        this.action.execute();
      } else {
        this.action.complete();
        this.action = null;
      }
    }
    else for (GuildRanger.Action localAction : this.actions) {
        if (localAction.isValid()) {
          this.action = localAction;
          break;
        }
      }

    return random(50, 100);
  }

  public void onFinish()
  {
  }

  private boolean inShootingArea() {
    return GuildRanger.GameConstants.SHOOTING_AREA.contains(new RSTile[] { getMyPlayer().getLocation() });
  }

  private boolean isAttackingRanger()
  {
    return (getMyPlayer().getInteracting() != null) && (getMyPlayer().getInteracting().getName() != null) && (getMyPlayer().getInteracting().getName().equals("Guard"));
  }

  private boolean canCompete()
  {
    return (this.settings.getSetting(156) == 0) || (this.interfaces.get(243).isValid());
  }

  private boolean hasArrowsInInventory() {
    return this.inventory.getCount(new int[] { 882 }) > 0;
  }

  private boolean isTargetInterfaceOpen() {
    return this.interfaces.get(325).isValid();
  }

  private boolean isPaymentInterfaceOpen() {
    return this.interfaces.get(236).isValid();
  }

  public void onRepaint(Graphics paramGraphics)
  {
    if ((this.game.isLoggedIn()) && (this.skills.getRealLevel(4) >= 40)) {
      Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
      localGraphics2D.setRenderingHints(GuildRanger.GameConstants.ANTI_ALIASING);
      if (this.startTime == 0L) {
        this.startTime = System.currentTimeMillis();
      }
      if (this.startXp == 0) {
        this.startXp = this.skills.getCurrentExp(4);
      }
      if (this.startLvl == 0) {
        this.startLvl = this.skills.getRealLevel(4);
      }
      if (this.action != null) {
        this.action.paint(localGraphics2D);
      }
      localGraphics2D.setColor(GuildRanger.GameConstants.BACKGROUND_COLOR);
      localGraphics2D.fillRect(10, 35, 205, 195);
      localGraphics2D.setColor(GuildRanger.GameConstants.TEXT_COLOR);
      localGraphics2D.drawRect(10, 35, 205, 195);
      localGraphics2D.drawString("GuildRanger by Vastico", 20, 55);
      localGraphics2D.drawString(Timer.format(System.currentTimeMillis() - this.startTime), 20, 75);

      localGraphics2D.drawString(this.action != null ? this.action.getDescription() : "Calculating...", 20, 95);

      localGraphics2D.drawString("XP Gained: " + (this.skills.getCurrentExp(4) - this.startXp), 20, 115);

      localGraphics2D.drawString("XP Per Hour: " + calculateXpPerHour(), 20, 135);
      localGraphics2D.drawString("Current Level: " + this.skills.getRealLevel(4), 20, 155);

      localGraphics2D.drawString("Levels Gained: " + (this.skills.getRealLevel(4) - this.startLvl), 20, 175);

      localGraphics2D.drawString("Current Score: " + this.settings.getSetting(157), 20, 195);
      int i = this.settings.getSetting(156);
      i = i >= 1 ? i - 1 : 0;
      localGraphics2D.drawString("Arrows Fired(10): " + i, 20, 215);
    }
  }

  private int calculateXpPerHour() {
    int i = this.skills.getCurrentExp(4) - this.startXp;
    return (int)(3600000.0D / (System.currentTimeMillis() - this.startTime) * i);
  }
}